// These examples are from the Electronics Cookbook by Simon Monk
//https://github.com/LSatan/SmartRC-CC1101-Driver-Lib
// mod by Little_S@tan
#include <ELECHOUSE_CC1101_SRC_DRV.h>

#include <Arduino.h>
#include <U8g2lib.h>
 //OLED部分
#ifdef U8X8_HAVE_HW_SPI
#include <SPI.h>
#endif
#ifdef U8X8_HAVE_HW_I2C
#include <Wire.h>
#endif


int gdo0;

//ADC
#define POT PA0 //Resistance of POT
#define Button_A PB12//计数按键
#define Button_B PB13//清理按键，同接收端
#define Button_C PB14//调制
#define Button_D PB15//频率
#define Led PC13//发送指示灯

//0.96OLED引脚定义
U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ PB6, /* data=*/ PB7, /* reset=*/ U8X8_PIN_NONE);   

void setup() 
{

    gdo0=PA2;//GDO0引脚定义,发送部分

    pinMode(POT,INPUT_ANALOG);//ADC引脚为模拟输入
    pinMode(Button_A,INPUT_PULLUP); //配置按键A为上拉输入
    pinMode(Button_B,INPUT_PULLUP);//配置按键B为上拉输入
    pinMode(Led,OUTPUT);  //配置LED为输出模式

    Serial.begin(9600);//波特率初始化9600
    
    u8g2.begin();                         //oled初始化

    if (ELECHOUSE_cc1101.getCC1101()){         // Check the CC1101 Spi connection.
    Serial.println("Connection OK");
    }else{
    Serial.println("Connection Error");
    }
  
    ELECHOUSE_cc1101.Init();               // must be set to initialize the cc1101!
    ELECHOUSE_cc1101.setGDO0(gdo0);         // set lib internal gdo pin (gdo0). Gdo2 not use for this example.
    ELECHOUSE_cc1101.setCCMode(1);       // set config for internal transmission mode.
    ELECHOUSE_cc1101.setModulation(0);  // set modulation mode. 0 = 2-FSK, 1 = GFSK, 2 = ASK/OOK, 3 = 4-FSK, 4 = MSK.
    ELECHOUSE_cc1101.setMHZ(433.92);   // Here you can set your basic frequency. The lib calculates the frequency automatically (default = 433.92).The cc1101 can: 300-348 MHZ, 387-464MHZ and 779-928MHZ. Read More info from datasheet.
    ELECHOUSE_cc1101.setSyncMode(2);  // Combined sync-word qualifier mode. 0 = No preamble/sync. 1 = 16 sync word bits detected. 2 = 16/16 sync word bits detected. 3 = 30/32 sync word bits detected. 4 = No preamble/sync, carrier-sense above threshold. 5 = 15/16 + carrier-sense above threshold. 6 = 16/16 + carrier-sense above threshold. 7 = 30/32 + carrier-sense above threshold.
 // ELECHOUSE_cc1101.setPA(10);      // set TxPower. The following settings are possible depending on the frequency band.  (-30  -20  -15  -10  -6    0    5    7    10   11   12) Default is max!
    ELECHOUSE_cc1101.setCrc(1);     // 1 = CRC calculation in TX and CRC check in RX enabled. 0 = CRC disabled for TX and RX.
    Serial.println("Tx Mode");

    digitalWrite(Led,HIGH);
}

byte co;//发送次数

static int i=0;//配置静态变量，记录按键计数
// static int lastButtonState = 1; // 累加按键，高电平表示未按下

// static int modulation_mode = 0; // 0 = 2-FSK, 1 = GFSK, 2 = ASK/OOK, 3 = 4-FSK, 4 = MSK.
// static int lastButtonCState = 1; // 调制按键，高电平表示未按下

// static int frequency_count = 0; // 记录频率累加值
// static unsigned long lastDebounceTime = 0; // 上次去抖动的时间
// const unsigned long debounceDelay = 50; // 去抖动的时间
// static int lastButtonDState = 1 ; // 按键D上一次状态
// static unsigned long lastPressTime = 0; // 记录上次按下的时间
// static int pressCount = 0; // 按下次数


char transmitt_char[20];//定义发送字符数组
//ADC Init
int analogValue;//获取ADC值的变量
char message[20];  // Create a char array to hold the message
char sum[30]; //按键计数的数组
//Button
byte button1,button2;//定义按键变量

void loop() 
{

  // co++;
  // snprintf(transmitt_char, sizeof(transmitt_char), "Hello World %d", co);  
  // ELECHOUSE_cc1101.SendData(transmitt_char,20);
  // delay(100);

  // //ADC POT Voltage
  //   analogValue=analogRead(POT);
  //   //Serial.print(analogValue);
  //   ///Serial.print(',');
  // snprintf(message, sizeof(message), "POT Value: %d", analogValue);
  // 使用 snprintf 格式化消息
  snprintf(sum, sizeof(sum), "%d", i);//将按键计数转换为字符串
  ELECHOUSE_cc1101.SendData(sum,strlen(sum));//发送按键计数
  // ELECHOUSE_cc1101.SendData(message,strlen(message));
  // delay(100);

  //Button Press
    button1=digitalRead(Button_A); // 读取计数按键状态
    button2=digitalRead(Button_B);// 读取清零按键状态

  if (button1 == 0 ) // 检测计数按键按下
  {
    delay(10);
    if(button1 == 0 )
    {
      i++; // 累加计数
      // ELECHOUSE_cc1101.SendData("Count", i);
      // Serial.println("Count: " + String(i));
      // delay(100); 
    }


  }

  if (button2 == 0)//检测清零按键按下
  {
    delay(10);
    if(button2 == 0)
    {
      i=0;//清零
      // while(button2==0);
      // ELECHOUSE_cc1101.SendData("Clean",i);
      // Serial.println("Clean");
      // delay(100);
    }
  }

      // OLED显示函数
    u8g2.clearBuffer(); // 清除oled缓存
    u8g2.setFont(u8g2_font_ncenB08_tr); // 设置字体
    u8g2.drawStr(0, 10, "Hello World!"); // 绘制字符串
    u8g2.setCursor(0, 20); // 设置光标位置
    u8g2.print("Count: "); // 显示当前计数
    u8g2.print(i); 
    // u8g2.setCursor(0, 30); // 显示频率计数
    // u8g2.print("Freq: ");
    // u8g2.print(frequency_count); // 显示累加的频率值
    u8g2.sendBuffer(); // 刷新显示

    // delay(100);
}
